﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class formkhachhang : Form
    {
        public formkhachhang()
        {
            InitializeComponent();
        }
        quanlybansachDataContext db = new quanlybansachDataContext();
        khachhang_bll kh = new khachhang_bll();
        //==========================================================
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            dgv.DataSource = from kh in db.khachhangs select kh;
            dgv.DataBindings.Clear();
        }
        //==========================================================
        private void btnhaplai_Click(object sender, EventArgs e)
        {
            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }
        //==========================================================
        private void btthoat_Click(object sender, EventArgs e)
        {
            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }
        //==========================================================
        private void btthem_Click(object sender, EventArgs e)
        {
            khachhang obj = new khachhang();
            bool check = kh.kiemtrakhachhang(txtmakh.Text);
            if (check)
            {
                obj.makh = txtmakh.Text;
                obj.ten = txtten.Text;
                obj.email = txtemail.Text;
                obj.sđt = txtsdt.Text;
                obj.diachi = txtdiachi.Text;
                kh.themkhachhang(obj);
                lblloi.Text = "Thêm thành công";
            }
            else
            {
                MessageBox.Show("Thêm thất bại!","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            db.SubmitChanges();
            hienthi();
        }
        //===========================================================
        private void btsua_Click(object sender, EventArgs e)
        {
            bool check = kh.kiemtrakhachhang(txtmakh.Text);
            if (!check)
            {
                khachhang khachhang = db.khachhangs.Where(khach => khach.makh == txtmakh.Text).FirstOrDefault();
                khachhang.ten = txtten.Text;
                khachhang.email = txtemail.Text;
                khachhang.sđt = txtsdt.Text;
                khachhang.diachi = txtdiachi.Text;
                kh.suakhachhang(khachhang);
                db.SubmitChanges();
                lblloi.Text = "Sủa thành công!";
            }
            else MessageBox.Show("Sửa thất bại","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            hienthi();
        }
        //============================================================
        private void btxoa_Click(object sender, EventArgs e)
        {
            bool check = kh.kiemtrakhachhang(txtmakh.Text);
            if (!check)
            {
                khachhang k = db.khachhangs.Where(khach=>khach.makh==txtmakh.Text).FirstOrDefault();
                kh.xoakhachhang(txtmakh.Text);
                db.SubmitChanges();
                lblloi.Text = "Xóa thành công!";
            }
            else
            {
                MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
    
            hienthi();
        }
        //=============================================================
        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }

        private void formkhachhang_Load(object sender, EventArgs e)
        {
            hienthi();
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;//n is the location of the selected line  
            txtmakh.Text = dgv.Rows[n].Cells[0].Value.ToString();
            txtten.Text = dgv.Rows[n].Cells[1].Value.ToString();
            txtemail.Text = dgv.Rows[n].Cells[2].Value.ToString();
            txtsdt.Text = dgv.Rows[n].Cells[3].Value.ToString();
            txtdiachi.Text = dgv.Rows[n].Cells[4].Value.ToString();
        }


        private void bttimma_Click(object sender, EventArgs e)
        {
            if (kh.timkiemma(txtmakh.Text) == true)
            {
                dgv.DataSource=from k in db.khachhangs where k.makh==txtmakh.Text select k;
                lblloi.Text="Đã tìm thấy!";
            }
            else

                MessageBox.Show("Không tìm thấy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void bttimten_Click(object sender, EventArgs e)
        {
            if (kh.timkiemten(txtten.Text) == true)
            {
                dgv.DataSource = from k in db.khachhangs where k.ten == txtten.Text select k;
                lblloi.Text = "Đã tìm thấy!";
            }
            else

                MessageBox.Show("Không tìm thấy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void bttimemail_Click(object sender, EventArgs e)
        {
            if (kh.timkiememail(txtemail.Text) == true)
            {
                dgv.DataSource = from k in db.khachhangs where k.email == txtemail.Text select k;
                lblloi.Text = "Đã tìm thấy!";
            }
            else

                MessageBox.Show("Không tìm thấy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void bttimsdt_Click(object sender, EventArgs e)
        {
            if (kh.timkiemsdt(txtsdt.Text) == true)
            {
                dgv.DataSource = from k in db.khachhangs where k.sđt == txtsdt.Text select k;
                lblloi.Text = "Đã tìm thấy!";
            }
            else

                MessageBox.Show("Không tìm thấy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void bttimdiachi_Click(object sender, EventArgs e)
        {
            if (kh.timkiemdiachi(txtdiachi.Text) == true)
            {
                dgv.DataSource = from k in db.khachhangs where k.diachi == txtdiachi.Text select k;
                lblloi.Text = "Đã tìm thấy!";
            }
            else
                MessageBox.Show("Không tìm thấy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        

        
    }
}
